package com.u.servlet;

import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.u.Repiar.RepairItem_info;
import com.u.character.Role;
import com.u.character.Station;
import com.u.eventInfo.Event;
import com.u.eventInfo.EventInfo;
import com.u.tool.MysqlUtil;

/**
 * Servlet implementation class NewEvent
 */
@WebServlet("/NewEvent")
public class NewEvent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewEvent() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String event_itemid = request.getParameter("event_item");
		String event_description = request.getParameter("event_description");
		String pic_address = request.getParameter("pic_address");
		String applier_name = request.getParameter("applier_name");
		String applier_tel = request.getParameter("applier_tel");
		MysqlUtil db = new MysqlUtil();
		String sql = "select * from y_r.repair_item where item_id=" + event_itemid;
		db.connection();
		List<RepairItem_info> item_list = db.query(RepairItem_info.class, sql);
		String repairer = item_list.get(0).getRepairer();
		HttpSession session = request.getSession();
		Role role = (Role) session.getAttribute("Role");
		if((role.getUsertype()).equals("station")) {
			
			db.connection();
			sql = "SELECT * FROM y_r.repair_item where item_id = '"+event_itemid+"'";
			List<RepairItem_info> list = db.query(RepairItem_info.class, sql);
			RepairItem_info ri = list.get(0);
			String event_type = ri.getType();
			String event_item = ri.getItem_name();
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			String event_date = format.format(date.getTime());
			
			Calendar c = Calendar.getInstance();
			String event_id = "NO" + event_itemid + c.get(Calendar.DATE) + c.get(Calendar.MILLISECOND);
			
			EventInfo eventInfo = new EventInfo();
			eventInfo.setApplier_name(applier_name);
			eventInfo.setApplier_tel(applier_tel);
			eventInfo.setEvent_date(event_date);
			eventInfo.setEvent_description(event_description);
			eventInfo.setEvent_id(event_id);
			eventInfo.setEvent_item(event_item);
			eventInfo.setPicaddress(pic_address);
			
			Station station = new Station(role);
			String station_id = station.getStationCode();
			String station_name = station.getStationName();
			System.out.println("Insert station name:" + station_id);
	
			Event event = new Event();
			event.setEvent_id(event_id);
			event.setEvent_type(event_type);
			event.setEvent_item(event_item);
			event.setEvent_date(event_date);
			event.setItem_id(event_itemid);
			event.setStatus("uploaded");
			event.setStation_id(station_id);
			event.setStation_name(station_name);
			event.setRepairer(repairer);
			db.connection();
			db.add(event);
			db.connection();
			db.add(eventInfo);
			
		}	
	response.sendRedirect("/r/eventList.html");
	}	
}
